Imports System.Net
Imports System.IO.Stream
Imports System.Text

Imports System.IO
Imports System.IO.FileStream


Public Class FrmMapa


    Dim mLatitud As String
    Dim mlongitud As String


    Public Property Latitud() As String
        Get
            Return mLatitud
        End Get
        Set(ByVal value As String)
            mLatitud = value
        End Set
    End Property


    Public Property Longitud() As String
        Get
            Return mlongitud
        End Get
        Set(ByVal value As String)
            mlongitud = value
        End Set
    End Property



    Private Sub FrmMapa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        DibujarMapa(Me.Latitud, Me.Longitud)

    End Sub


    Public Sub DibujarMapa(ByVal Lat As String, ByVal Lon As String)

        Dim Buffer(40000) As Byte
        Dim Size As Integer = 0
        Dim BytesRead As Integer = 0

        Lat = "-" & Lat
        Lon = "-" & Lon

        Dim Url As String
        Url = "http://maps.google.com/maps/api/staticmap?center=&" & Lat & "," & Lon & "&"
        Url += "zoom=15&"
        Url += "size=400x300&"
        Url += "format=jpg&"
        Url += "maptype=roadmap&"
        Url += "markers=color:red|color:red|label:C|" & Lat & "," & Lon & "&"
        Url += "sensor=false&"
        Url += "key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA"

        Dim Req As HttpWebRequest = Req.Create(Url)
        Dim Res As HttpWebResponse = Req.GetResponse()
        Dim Contents As IO.Stream = Res.GetResponseStream()
        Dim BytesToRead As Integer = CInt(Buffer.Length)

        While BytesToRead > 0
            Size = Contents.Read(Buffer, BytesRead, BytesToRead)
            If Size = 0 Then Exit While
            BytesToRead -= Size
            BytesRead += Size
        End While

        Dim MapaGoogle As String = "C:\" & System.IO.Path.GetRandomFileName()

        'If My.Computer.FileSystem.FileExists("C:\download.jpg") Then
        '    My.Computer.FileSystem.DeleteFile("C:\download.jpg")
        'End If

        Dim Fstrm As New FileStream(MapaGoogle, FileMode.OpenOrCreate, FileAccess.Write)
        Fstrm.Write(Buffer, 0, BytesRead)
        Contents.Close()
        Fstrm.Close()
        Buffer = Nothing

        PictureBox1.Width = 400
        PictureBox1.Height = 300
        PictureBox1.Image = Image.FromFile(MapaGoogle)

    End Sub

End Class